﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace WFL.Office.Word
{
    public class TableCell
    {
        private readonly object _lock = new object();

        private WordRange _range;
        public WordRange Range
        {
            get
            {
                if (_range == null)
                {
                    lock (_lock) { if (_range == null) { _range = new WordRange(_cell.Range) { Document = Table.Document }; } }
                }
                return _range;
            }
        }

        public CellVerticalAlignment VerticalAlignment
        {
            get { return (CellVerticalAlignment)_cell.VerticalAlignment; }
            set { _cell.VerticalAlignment = (WdCellVerticalAlignment)value; }
        }

        /// <summary>
        /// 自动换行
        /// </summary>
        public bool WordWrap
        {
            get { return _cell.WordWrap; }
            set { _cell.WordWrap = value; }
        }

        /// <summary>
        /// 适应文字
        /// </summary>
        public bool FitText
        {
            get { return _cell.FitText; }
            set { _cell.FitText = value; }
        }

        public int RowIndex
        {
            get { return _cell.RowIndex; }
        }

        public int ColumnIndex
        {
            get { return _cell.ColumnIndex; }
        }

        public TableCell Next
        {
            get 
            {
                if (_cell.Next == null)
                {
                    return null;
                }
                return new TableCell(_cell.Next) { Table = Table }; 
            }
        }

        public float Width
        {
            get { return _cell.Width; }
            set { _cell.Width = value; }
        }

        public float Height
        {
            get { return _cell.Height; }
            set { _cell.Height = value; }
        }


        #region 边距

        public float TopPadding
        {
            get { return _cell.TopPadding; }
            set { _cell.TopPadding = value; }
        }

        public float BottomPadding
        {
            get { return _cell.BottomPadding; }
            set { _cell.BottomPadding = value; }
        }

        public float LeftPadding
        {
            get { return _cell.LeftPadding; }
            set { _cell.LeftPadding = value; }
        }

        public float RightPadding
        {
            get { return _cell.RightPadding; }
            set { _cell.RightPadding = value; }
        }

        #endregion


        private Cell _cell;

        public WordTable Table
        {
            get;
            internal set;
        }

        internal TableCell(Cell cell)
        {
            _cell = cell;
        }

        public void Merge(TableCell mergeTo)
        {
            _cell.Merge(mergeTo._cell);
        }

        public void Split(int numRows,int numColumns)
        {
            _cell.Split(numRows, numColumns);
        }

    }
}
